package com.supermap.dataservice.cloud.maker;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.maker.AdInfo;
import com.supermap.dubbo.api.cloud.maker.MakerService;
import com.supermap.dubbo.api.cloud.maker.Path;
import org.apache.logging.log4j.util.Strings;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2019年11月20 14:48
 * @email 1301584033@qq.com
 */
@Service
public class MakerServiceImpl implements MakerService {

    @Resource
    private MakerMapper makerMapper;

    @Override
    public JSONUtils getProductByArea(String areaCode, Integer limit) {
        if (Strings.isBlank(areaCode) || areaCode.equals("null") || limit == null) {
            return new JSONUtils().setMsg("获取最新产品【getProductByArea】的参数错误【areaCode:" + areaCode + ",limit:" + limit + "】，请确认参数！");
        }
        return new JSONUtils().setData(makerMapper.getProductByArea(areaCode, limit));
    }

    @Override
    public JSONUtils getSlidesShow(String areaCode, Integer limit) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");

        if (Strings.isBlank(areaCode)  || limit == null) {
            return new JSONUtils().setMsg("获取首页宣传广告【getSlidesShow】参数错误，areaCode:"+
                    areaCode+",limit:"+limit+",请确认参数！");
        }
        return new JSONUtils().setData(makerMapper.getIndexSlideInfo2(areaCode,limit));
    }

    @Override
    public JSONUtils getIndexSlideInfo(String areaCode,Integer limit) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");

        if(Strings.isBlank(areaCode) || areaCode.equals("null") || limit == null)
        {
            return new JSONUtils().setMsg("获取轮播图【getIndexSlideInfo】:参数错误，请确认参数！");
        }

        List<Map<String,Object>> dt = makerMapper.getIndexSlideInfo1(areaCode,limit);
        if(dt.size()>0)
        {
            return new JSONUtils().setData(dt);
        }
        return new JSONUtils().setData(makerMapper.getIndexSlideInfo2(areaCode,limit));
    }

    @Override
    public JSONUtils getTraceTypeList(String areaCode,String userId,String farm) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(makerMapper.getTraceTypeList(areaCode,userId,farm));
    }

    @Override
    public JSONUtils getTraceList(String areaCode,String userId,String farm,String subject) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(makerMapper.getTraceList(areaCode,userId,farm,subject));
    }

    @Override
    public Integer updateGoodsRead(String gtype,String gid,String genre) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if(Strings.isBlank(gtype) || Strings.isBlank(gid)|| Strings.isBlank(genre)) {
            return 0;
        }

        Integer cqNum= makerMapper.updateGoodsRead(gtype,gid,genre);
        return  cqNum;
    }

    @Override
    public JSONUtils getCountryTypeList(String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(makerMapper.getCountryTypeList(areaCode));
    }

    @Override
    public JSONUtils getCountryList(String areaCode,String tcode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(makerMapper.getCountryList(areaCode,tcode));
    }

    @Override
    public Integer updateCountryRead(String cid,String ctype) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if(Strings.isBlank(cid) || Strings.isBlank(ctype)) {
            return 0;
        }
        return  makerMapper.updateCountryRead(cid,ctype);
    }

    @Override
    public JSONUtils getAdSlideInfo(String areaCode, Integer state,Integer isuse,Integer limit) {
        if(Strings.isBlank(areaCode)||limit == null)
        {
            return  new JSONUtils().setMsg("获取启动页广告【getAdSlideInfo】：参数错误，请确认参数！");
        }

        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        return new JSONUtils().setData(makerMapper.getAdSlideInfo(areaCode,state,isuse,limit));
    }

    @Override
    public JSONUtils getOrderMenuByUserId(Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        return  new JSONUtils().setData(makerMapper.getOrderMenuByUserId(userId));
    }

    @Override
    public JSONUtils getExpertList(Integer userId, String expertName, Integer subjectId, String areaCode, Integer state, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        if(userId==null || page==null || size==null)
        {
            new JSONUtils().setMsg("专家列表(getExpertList)参数错误！userId:"+userId+",page:"+page+",size:"+size);
        }


        return  new JSONUtils().setData(makerMapper.getExpertList(userId,expertName,subjectId,areaCode,state,page,size));
    }

    @Override
    public JSONUtils putExpertAttention(Integer userId, Integer expertId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        makerMapper.putExpertAttention(userId,expertId);
        return new JSONUtils();
    }

    @Override
    public JSONUtils delExpertAttention(Integer userId, Integer expertId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        makerMapper.delExpertAttention(userId,expertId);
        return new JSONUtils();
    }

    @Override
    public JSONUtils putAdSlideInfo(AdInfo adInfo) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        makerMapper.putAdSlideInfo(adInfo);
        return new JSONUtils();
    }

    @Override
    public JSONUtils saveAdSlideInfo(AdInfo adInfo) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        makerMapper.saveAdSlideInfo(adInfo);
        return new JSONUtils();
    }

    @Override
    public JSONUtils delAdSlideInfo(Integer adInfoId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        makerMapper.delAdSlideInfo(adInfoId);
        return new JSONUtils();
    }

    @Override
    public  JSONUtils putMenuClick(String menuCode,Integer userId,String areaCode) {
        if(menuCode == null || menuCode == "null") return  new JSONUtils();

        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        makerMapper.putMenuClick(menuCode,userId,areaCode);
        return new JSONUtils();
    }

    @Override
    public JSONUtils getUserList(String roleCode, String userName, String areaCode, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
 /*       if(Strings.isBlank(roleCode)|| page==null|| size==null)
        {
            return  new JSONUtils().setMsg("农业主体列表【getUserList】：参数错误，请确认参数！");
        }*/

        return new JSONUtils().setData(makerMapper.getUserList(roleCode,userName,areaCode,page,size));
    }

    @Override
    public JSONUtils getProductGroupList(String areaCode, Integer templateId) {
        if(Strings.isBlank(areaCode))
        {
            return new JSONUtils().setMsg("产品组列表【getProductGroupList】参数错误,areaCode:null");
        }
        else if("86".equals(areaCode))
        {
            return new JSONUtils().setData(makerMapper.getProductGroupList86(templateId));
        }
        else
        {
            return new JSONUtils().setData(makerMapper.getProductGroupList(areaCode,templateId));
        }
    }

    @Override
    public JSONUtils getProductList(String groupIds) {
        return new JSONUtils().setData(makerMapper.getProductList(groupIds));
    }

    @Override
    public JSONUtils getWarningList(String areaCode,String cdate) {
        return new JSONUtils().setData(makerMapper.getWarningList(areaCode,cdate));
    }

    @Override
    public JSONUtils login(String loginName, String pwd) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        return new JSONUtils().setData(makerMapper.login(loginName));
    }

    @Override
    public JSONUtils getVisitorMenuList(String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        if(Strings.isBlank(areaCode))
        {
            new JSONUtils().setMsg("游客模式下【getVisitorMenuList】,参数错误,areaCode:null,请确认参数！");
        }
        return  new JSONUtils().setData(makerMapper.getVisitorMenuList(areaCode));
    }

    @Override
    public JSONUtils getPublicProductCount(String areaCode,String Ctype, Integer limit) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        if(Strings.isBlank(areaCode)||Strings.isBlank(Ctype))
        {
            return new JSONUtils().setMsg("公众服务产品(阅读、点赞、分享)数统计【getPublicProductCount】," +
                    "参数错误【areaCode:"+"areaCode,"+"Ctype:"+Ctype +"】,请确认参数！");
        } else if("86".equals(areaCode))
        {
            return  new JSONUtils().setData(makerMapper.getPublicProductCount86(Ctype,limit));
        }
        else
        {
            return  new JSONUtils().setData(makerMapper.getPublicProductCount(areaCode,Ctype,limit));
        }
    }

    @Override
    public JSONUtils getGoodsReadCount(String areaCode,String gtype,Integer limit) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        if(Strings.isBlank(areaCode)||Strings.isBlank(gtype))
        {
            return new JSONUtils().setMsg("好产品访问量、点赞数、分享数统计【getGoodsReadCount】," +
                    "参数错误【areaCode:"+"areaCode,"+"gtype:"+gtype +"】,请确认参数！");
        }
        return  new JSONUtils().setData(makerMapper.getGoodsReadCount(areaCode,gtype,limit));
    }

    @Override
    public JSONUtils getCountryReadCount(String areaCode,String ctype,Integer limit) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2 ");
        JSONUtils josn =  new JSONUtils();
        if(Strings.isBlank(areaCode)||Strings.isBlank(ctype))
        {
          return  new JSONUtils().setMsg("美丽乡村访问量、点赞数、分享数统计【getCountryReadCount】," +
                    "参数错误【areaCode:"+"areaCode,"+"gtype:"+ctype +"】,请确认参数！");
        }
        return  new JSONUtils().setData(makerMapper.getCountryReadCount(areaCode,ctype,limit));
    }

}
